/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.twitter.acount;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tompai.datagather.model.TwitterAcounts;

/**   
* @date 2016年8月9日 下午12:24:06 
* @version   
* @since JDK 1.8  
*/
public class FollowersResponse {

	public static final Logger LOG = LoggerFactory.getLogger(FollowersResponse.class);
	
	private String min_position;
	private boolean has_more_items;
	private String items_html;
	private long new_latent_count;
	
	public FollowersResponse(){
		// do some thing//
	}
	
	public FollowersResponse(String min_position, boolean has_more_items, String items_html, long new_latent_count) {
		this.min_position = min_position;
		this.has_more_items = has_more_items;
		this.items_html = items_html;
		this.new_latent_count = new_latent_count;
	}
	
	
	public boolean isHas_more_items() {
		return has_more_items;
	}
	

	public void setHas_more_items(boolean has_more_items) {
		this.has_more_items = has_more_items;
	}
	
	public String getItems_html() {
		return items_html;
	}
	
	public void setItems_html(String items_html) {
		this.items_html = items_html;
	}
	
	public String getMin_position() {
		return min_position;
	}
	
	public void setMin_position(String min_position) {
		this.min_position = min_position;
	}
	
	public long getNew_latent_count() {
		return new_latent_count;
	}
	
	public void setNew_latent_count(long new_latent_count) {
		this.new_latent_count = new_latent_count;
	}
	
	public  List<TwitterAcounts> getTweets() {
		final List<TwitterAcounts> tweetBeans = new ArrayList<TwitterAcounts>();
		Document doc = Jsoup.parse(items_html);
		for (Element el : doc.select("div.profilecard-userfields")) {
			String id = el.attr("data-item-id");
			String profile = null;
			String text = null;
			//String userId = null;
			String userScreenName = null;
			String userLink=null;
			String userName = null;
			//Date createdAt = null;
			//int retweets = 0;
			//int favourites = 0;
			
			try {
				userName = el.select("a.ProfileNameTruncated-link.u-textInheritColor.js-nav.js-action-profile-name").text();
				//LOG.info("userName:"+userName);
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
			
			try {
				//ProfileNameTruncated-link u-textInheritColor js-nav js-action-profile-name//
				//
				//userScreenName = el.select("a.ProfileNameTruncated-link.u-textInheritColor.js-nav.js-action-profile-name").text();
				userScreenName=el.select("b.u-linkComplex-target").text();
				//a.ProfileCard-screennameLink.u-linkComplex.js-nav
				//userScreenName=el.select("a.ProfileCard-screennameLink.u-linkComplex.js-nav").text();
				//userScreenName="@"+userScreenName;
				//LOG.info("userScreenName: @"+userScreenName);
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
			
			try {
				profile = el.select("p.ProfileCard-bio.u-dir.js-ellipsis").text();
				//LOG.info("profile:"+profile);
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
			
			////TEST=======================================
			//Get The Acount Link//
			try {
				String twitterAddress="https://twitter.com/";
				userLink=twitterAddress+userScreenName;
				//LOG.info("userLink:"+userLink);
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
			////TEST=========================================
			//LOG.info("---------------------------------------");
			TwitterAcounts tweetBean = new TwitterAcounts(userName, userScreenName,profile);
			//////////////////////////////////////////////
			if (tweetBean.getProfileName() != null) {
				tweetBeans.add(tweetBean);
			}
		}
		return tweetBeans;
	}
}
